<?php
	session_start();
	require('core/config.php');
	//上传评论
	if($_GET['action']=='upremark'){
		$authorid = isset($_SESSION['userid'])?$_SESSION['userid']:0;
		if(subRemark($_POST['artid'],$authorid,$_POST['name'],$_POST['content'],time(),$_POST['email'],$_POST['url'])==true){
			echo 1;
		}
	//注册
	}elseif($_GET['action']=='register'){
		$data = array();
		$data['username'] = $_POST['username'];
		$data['password'] = $_POST['password'];
		if(isset($_POST['qq'])){
			$data['qq'] = $_POST['qq'];
		}else{
			$data['qq'] = '';
		}
		$data['isadmin'] = 0;
		$data['regtime'] = time();
		$check = checkUserData($data);
		if($check!=1){
			echo $check;
			exit(0);
		}
		$data['password'] = md5($_POST['password']);
		if(addUser($data)==true){
			$user = getUserByName($_POST['username']);
			$_SESSION['userid'] = $user['uid'];
			setcookie('username',$_POST['username'],time()+60*60*24*30);
			setcookie('md5password',md5($_POST['password']),time()+60*60*24*30);
			echo 1;
		}else{
			echo 6;
		}
	}elseif($_GET['action']=='checkname'){
		if(checkHasName($_POST['username'])==true){
			echo 1;
		}
	}
	//登录
	elseif($_GET['action']=='login'){
		if(loginSub($_POST['username'],md5($_POST['password']))==true){
			$user = getUserByName($_POST['username']);
			$_SESSION['userid'] = $user['uid'];
			setcookie('username',$_POST['username'],time()+60*60*24*30);
			setcookie('md5password',md5($_POST['password']),time()+60*60*24*30);
			echo "<script>location.replace('index.php?')</script>";
		}else{
			echo "<script>location.replace('login.php?')</script>";
		}
	//退出登录
	}elseif($_GET['action']=='logout'){
		unset($_SESSION['userid']);
		setcookie("username", "", time() - 3600);
		setcookie("md5password", "", time() - 3600);
		echo "<script>location.replace('index.php?')</script>";
	//上传头像
	}elseif($_GET['action']=='subhead'){
			$pass = checkFile($_FILES);
			if($pass==0){
				$base = time();
				$destination = "./source/head/";
				foreach($_FILES as $key=>$value){
					$oldimg = extractImageSizeAndResponse($value['tmp_name']);
					if($oldimg==false){
						exit(0);
					}
					$newimg = imagecreatetruecolor(100,100); 
					imagecopyresampled($newimg, $oldimg[3], 0, 0, 0, 0, 100, 100, $oldimg[0], $oldimg[1]); 
					do{
						$base++;
						$name = explode('.',$value['name']);
						$name = $base.'.'.$name[count($name)-1];
						$des = $destination.$name;
					}while(is_file($des));
					if($oldimg[2]==1){
						imagegif($newimg,$des);
					}else if($oldimg[2]==2){
						imagejpeg($newimg,$des);
					}else if($oldimg[2]==3){
						imagepng($newimg,$des);
					}
				}		
			}else{
				exit(0);
			}
			updateUserDate($_SESSION['userid'],array('headpic'),array($name));
			echo $name;
	//修改用户数据
	}elseif($_GET['action']=='updatedata'){
		$data = array();
		$key = array();
		if(loginSubById($_SESSION['userid'],md5($_POST['oldpassword']))!=true){
			echo 7;
			exit(0);
		}
		if(strlen($_POST['password'])>0){
			$data['password'] = $_POST['password'];
			array_push($key, 'password');
		}
		if(strlen($_POST['qq'])>0){
			$data['qq'] = $_POST['qq'];
			array_push($key, 'qq');
		}
		if(strlen(isset($_POST['mail']))>0){
			$data['mail'] = $_POST['mail'];
			array_push($key, 'email');
		}
		if(strlen($_POST['website'])>0){
			$data['website'] = $_POST['website'];
			array_push($key, 'website');
		}
		$check = checkUpdateData($data);
		if(strlen($_POST['password'])>0){
			$data['password'] = md5($_POST['password']);
		}
		if($check!=1){
			echo $check;
			exit(0);
		}
		if(updateUserDate($_SESSION['userid'],$key,$data)==true){
			echo 1;
		}else{
			echo 6;
		}

	}
	//获取评论页数
	elseif($_GET['action']=='getremarkpage'){
		$maxremark = (int)getConfig('maxremark');
		$pagenum = (int)((getRemarkByArtid($_GET['artid'],0,0,1)-1)/$maxremark)+1;
		$res = array();
		$pagearr = array();
		for($i=1;$i<=$pagenum;$i++){
			if($i>$pagenum){
				break;
			}
			array_push($pagearr, $i);				
		}
		array_push($res,$pagearr);
		array_push($res,$pagenum);

		$page = isset($_GET['page'])?$_GET['page']:1;

		$remark = getRemarkByArtid($_GET['artid'],(($page-1)*$maxremark),$maxremark);
		foreach($remark as $key=>$value){
			$remark[$key]['pubtime'] = turnTimeToDate($remark[$key]['pubtime']);
			if($value['authorid']!=0){
				$user = getUserById($value['authorid']);
				$remark[$key]['name'] = $user['username'];
				$remark[$key]['email'] = $user['email'];
				$remark[$key]['url'] = $user['website'];
				$remark[$key]['headpic'] = $user['headpic'];
			}
		}
		array_push($res,$remark);

		echo json_encode($res);
	}
	else{
		print_r($_POST);
	}
?>